
package com.gamatechno.solodestinationnew;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.gamatechno.solodestinationnew.utils.Utils;
import com.gamatechno.solodestinationnew.utils.Webservice;

public class RateActivity extends BaseActivity implements OnClickListener {

    private TextView title;
    private int locId;
    private int cat;
    private Bundle data;
    private RatingBar ratingBar;
    private EditText etComment;
    private Button btnKomen;
    private ProgressDialog pDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rate);

        getSupportActionBar().setDisplayHomeAsUpEnabled(false);

        data = getIntent().getExtras();

        pDialog = new ProgressDialog(this);
        pDialog.setMessage("Submit Comment");

        title = (TextView) findViewById(R.id.title);
        ratingBar = (RatingBar) findViewById(R.id.rate_user);
        etComment = (EditText) findViewById(R.id.et_input_komentar);
        btnKomen = (Button) findViewById(R.id.btn_komen);

        if (data != null) {
            Utils.TRACE("RateActivity", "onCreate : " + data.getString("title"));
            title.setText(data.getString("title"));
            locId = data.getInt("id");
            cat = data.getInt("cat");
        }

        btnKomen.setOnClickListener(this);

        String userName = Utils.getDataStringFromSP(this, Utils.PREF_USERNAME);
        Toast.makeText(this, "" + userName, Toast.LENGTH_SHORT).show();
        Utils.TRACE("RateActivity", "onCreate : username : " + userName);
    }

    private void postKomentar(String URL) {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,
                URL,
                null,
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject obj) {
                        Utils.TRACE("RateActivity.postKomentar(...).new Listener() {...}",
                                "onResponse : " + obj.toString());
                        Log.d("pratama", "" + obj.toString());
                        pDialog.dismiss();
                        try {
                            if (obj.getJSONObject("result").getBoolean("msg")) {
                                Toast.makeText(RateActivity.this, "Success", Toast.LENGTH_SHORT)
                                        .show();
                                RateActivity.this.finish();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        pDialog.dismiss();
                        Log.d("pratama", "" + error.toString());
                        Utils.TRACE("RateActivity.postKomentar(...).new ErrorListener() {...}",
                                "onErrorResponse : " + error.getMessage());
                        Utils.showVolleyError(RateActivity.this, error);
                    }
                });
        BaseApplication.getInstance().addToRequestQueue(request, "rate");
        pDialog.show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        // getMenuInflater().inflate(R.menu.rate, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v == btnKomen) {
            String fbID = Utils.getDataStringFromSP(this, Utils.PREF_FB_ID);
            String userName = Utils.encodeString(Utils.getDataStringFromSP(this,
                    Utils.PREF_USERNAME));

            switch (cat) {
                case MainActivity.CATEGORY_HOTEL:
                    postKomentar(Webservice.POST_COMMENT_HOTEL(
                            fbID,
                            locId,
                            ratingBar.getRating(),
                            Utils.encodeString(etComment.getText().toString()),
                            userName));

                    break;
                case MainActivity.CATEGORY_KULINER:
                    postKomentar(Webservice.POST_COMMENT_KULINER(
                            fbID,
                            locId,
                            ratingBar.getRating(),
                            Utils.encodeString(etComment.getText().toString()),
                            userName));
                    break;
                case MainActivity.CATEGORY_TOURISM:
                    postKomentar(Webservice.POST_COMMENT_WISATA(
                            fbID,
                            locId,
                            ratingBar.getRating(),
                            Utils.encodeString(etComment.getText().toString()),
                            userName));
                    break;
                case MainActivity.CATEGORY_SHOPPING:
                    postKomentar(Webservice.POST_COMMENT_SHOPPING(
                            fbID,
                            locId,
                            ratingBar.getRating(),
                            Utils.encodeString(etComment.getText().toString()),
                            userName));
                    break;
                case MainActivity.CATEGORY_HANDYCRAFT:
                    postKomentar(Webservice.POST_COMMENT_KERAJINAN(
                            fbID,
                            locId,
                            ratingBar.getRating(),
                            Utils.encodeString(etComment.getText().toString()),
                            userName));
                    break;
                default:
                    break;
            }
        }

    }
}
